/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.article.dao;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("siteArticleDao")
public class SiteArticleDao {

	@Autowired
	private Constants constants;

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库

	public boolean addSiteArticle(Map<String, Object> map) {

		map.put("articleId", Sequence.nextId());
		map.put("pubTime", new Date());
		map.put("createTime", new Date());
		map.put("isDelete", 0);
		map.put("readCount", 0);
		map.put("isPub", 0);

		String sql = "insert into "
				+ constants.getCurrentDB()
				+ "site_article "
				+ " (articleId,nodeId,title,subtitle,content,picLink,author,pubTime,readCount,isRecom,isTop,isPub,createTime,isDelete,articleType) "
				+ " values "
				+ " (:articleId,:nodeId,:title,:subtitle,:content,:picLink,:author,:pubTime,:readCount,:isRecom,:isTop,:isPub,:createTime,:isDelete,:articleType)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {

		return "" + " t.articleId," + " t.nodeId," + " t.title," + " t.subtitle," + " t.content," + " t.picLink,"
				+ " t.author," + " t.pubTime," + " t.readCount," + " t.isRecom," + " t.isTop," + " t.isPub,"
				+ " t.createTime ";
	}

	public String getColumnList() {

		return "" + " t.articleId," + " t.title," + " t.author," + " t.isRecom," + " t.isTop," + " t.isPub,"
				+ " t.pubTime ";
	}

	public PageObject querySiteArticleList(Map<String, Object> map) {

		String sql = "select " + getColumnList() + " from " + constants.getCurrentDB() + "site_article t where 1=1 ";

		if (null != map.get("articleId") && StringUtils.isNotEmpty(map.get("articleId").toString())) {
			sql = sql + " and t.articleId  = " + map.get("articleId") + "";
		}

		if (null != map.get("nodeId") && StringUtils.isNotEmpty(map.get("nodeId").toString())) {
			sql = sql + " and t.nodeId  = " + map.get("nodeId") + "";
		}

		if (null != map.get("title") && StringUtils.isNotEmpty(map.get("title").toString())) {
			sql = sql + " and t.title  like '%" + map.get("title") + "%' ";
		}

		if (null != map.get("subtitle") && StringUtils.isNotEmpty(map.get("subtitle").toString())) {
			sql = sql + " and t.subtitle  like '%" + map.get("subtitle") + "%' ";
		}

		if (null != map.get("author") && StringUtils.isNotEmpty(map.get("author").toString())) {
			sql = sql + " and t.author  = '" + map.get("author") + "'";
		}

		if (null != map.get("isTop") && StringUtils.isNotEmpty(map.get("isTop").toString())) {
			sql = sql + " and t.isTop  = " + map.get("isTop") + "";
		}

		if (null != map.get("isPub") && StringUtils.isNotEmpty(map.get("isPub").toString())) {
			sql = sql + " and t.isPub  = " + map.get("isPub") + "";
		}

		if (null != map.get("isRecom") && StringUtils.isNotEmpty(map.get("isRecom").toString())) {
			sql = sql + " and t.isRecom  = " + map.get("isRecom") + "";
		}

		if (null != map.get("pubTime") && StringUtils.isNotEmpty(map.get("pubTime").toString())) {
			sql = sql + " and t.pubTime >= " + map.get("pubTimeBegin") + "";
			sql = sql + " and t.pubTime <= " + map.get("pubTimeEnd") + "";
		}

		if (null != map.get("beginDate") && StringUtils.isNotEmpty(map.get("beginDate").toString())) {
			sql = sql + " and t.pubTime >= '" + map.get("beginDate") + " 00:00:00'";
		}

		if (null != map.get("endDate") && StringUtils.isNotEmpty(map.get("endDate").toString())) {
			sql = sql + " and t.pubTime <= '" + map.get("endDate") + " 23:59:59'";
		}

		if (null != map.get("createTime") && StringUtils.isNotEmpty(map.get("createTime").toString())) {
			sql = sql + " and t.createTime >= " + map.get("createTimeBegin") + "";
			sql = sql + " and t.createTime <= " + map.get("createTimeEnd") + "";
		}
		if (null != map.get("isDelete") && StringUtils.isNotEmpty(map.get("isDelete").toString())) {
			sql = sql + " and t.isDelete  = " + map.get("isDelete") + "";
		}
		if (null != map.get("articleType") && StringUtils.isNotEmpty(map.get("articleType").toString())) {
			sql = sql + " and t.articleType  = " + map.get("articleType") + "";
		}

		sql = sql + " order by isPub desc, t.pubTime desc";

		PageObject po = baseDAO.queryForMPageList(sql, new Object[] {}, map);

		return po;
	}

	public boolean updateSiteArticle(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_article set ";

		if (null != map.get("title") && StringUtils.isNotEmpty(map.get("title").toString())) {
			sql = sql + " title=:title,";
		}

		if (null != map.get("subtitle") && StringUtils.isNotEmpty(map.get("subtitle").toString())) {
			sql = sql + " subtitle=:subtitle,";
		}

		if (null != map.get("author") && StringUtils.isNotEmpty(map.get("author").toString())) {
			sql = sql + "  author=:author,";
		}

		if (null != map.get("picLink") && StringUtils.isNotEmpty(map.get("picLink").toString())) {
			sql = sql + "  picLink=:picLink,";
		}

		if (null != map.get("content") && StringUtils.isNotEmpty(map.get("content").toString())) {
			sql = sql + "  content=:content,";
		}

		if (null != map.get("isTop") && StringUtils.isNotEmpty(map.get("isTop").toString())) {
			sql = sql + "  isTop=:isTop,";
		}

		if (null != map.get("isRecom") && StringUtils.isNotEmpty(map.get("isRecom").toString())) {
			sql = sql + "  isRecom=:isRecom,";
		}

		if (null != map.get("isPub") && StringUtils.isNotEmpty(map.get("isPub").toString())) {
			sql = sql + "  isPub=:isPub,";
		}

		if (null != map.get("pubTime") && StringUtils.isNotEmpty(map.get("pubTime").toString())) {
			sql = sql + " pubTime=:pubTime,";
		}

		if (null != map.get("articleType") && StringUtils.isNotEmpty(map.get("articleType").toString())) {
			sql = sql + "  articleType=:articleType,";
		}

		sql = sql.substring(0, sql.length() - 1);
		sql = sql + " where articleId=:articleId";
		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteArticle(Map<String, Object> map) {

		String sql = "delete from " + constants.getCurrentDB() + "site_article where articleId in ("
				+ map.get("articleId") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean delSiteArticle(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_article set " + " isDelete = 1"
				+ " where articleId=:articleId";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean pubArticle(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_article set " + " pubTime=:pubTime," + " isPub = "
				+ map.get("status") + " where articleId in (" + map.get("ids") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean setValue(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_article set ";
		if (null != map.get("isTop") && StringUtils.isNotEmpty(map.get("isTop").toString())) {
			sql = sql + "  isTop=:isTop,";
		}

		if (null != map.get("isRecom") && StringUtils.isNotEmpty(map.get("isRecom").toString())) {
			sql = sql + "  isRecom=:isRecom,";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql = sql + " where articleId in (" + map.get("ids") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	@SuppressWarnings("unchecked")
	public Map getById(Map<String, Object> map) {

		String sql = "select " + getColumns() + " from " + constants.getCurrentDB()
				+ "site_article t where t.articleId = " + map.get("articleId") + "";

		return baseDAO.queryForMap(sql);
	}

	@SuppressWarnings("unchecked")
	public Map getInfoByNodeId(Map<String, Object> map) {

		String sql = "select " + getColumns() + " from site_article t where t.articleType = 2 and t.nodeId = "
				+ map.get("nodeId") + "";

		return baseDAO.queryForMap(sql);
	}

	///////////////////////////////////////////////////////////////
	@SuppressWarnings("unchecked")
	public Map getUsById() {

		String sql = "select articleId,content from " + constants.getCurrentDB()
				+ "site_article t where t.articleId = 0";

		return baseDAO.queryForMap(sql);
	}

	public boolean updateUs(Map<String, Object> map) {
		String sql = "update " + constants.getCurrentDB() + "site_article set  content=:content";

		sql = sql + " where articleId=:articleId";
		return baseDAO.executeNamedCommand(sql, map);
	}
}
